<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class HomeBlock extends BaseAction {

    /**
     * 访问日志
     */
    public function accessBlock() {
        
        $uhash = getRequestUhash();
        $url = $this->getUrl($uhash);
        if($url == null) {
            $this->redirect('/');
        }
        
        $ip = getIp();

        if (empty($_SESSION['user']['id'])) {
            $uid = 0;
        } else {
            $uid = $_SESSION['user']['id'];
        }


         //记录省份
        $ip = getIp();
        if($ip == '127.0.0.1' || $ip == '::1'){
            $local['province']  = '本机';
        }elseif(empty($local['province'])) {
            $local['province']  = '未知';
        }else{
            $local = getLocal($ip);
        }

        //FileWrite::writeTableProvince($uid, $uhash, $local['province']);
        $sql    = "INSERT {#prefix#}url_province (uid,uhash, name) values (?,?,?) ON DUPLICATE KEY UPDATE click=click+1";
        $pro    = $local['province'];
        DAO_Finder::exec($sql, DB_USER, array($uid, $uhash, $pro));

        //更新点击

        $sqlQuery   = "update `u22_url` set total_pv =total_pv+1 where uhash='$uhash'";
        DAO_Finder::exec($sqlQuery);

        //记录PV
        //$acc    = DB_ACCESS::writeAccess($uhash);
        //$sql = "call updateCountVoid('$uhash','$ip',$uid); " . getBr();

        $total  = DAO_Finder::findCount(DB_ACCESS, array('uhash' => $uhash,'ip' => $ip), 'id');
        $uid = 0;
        if($total == 0) {
            $sql    = "UPDATE `{#prefix#}url` SET total_ip=total_ip +1 WHERE `uhash` = ?";
            DAO_Finder::exec($sql, DB_USER, array($uhash));

            $sql    = "INSERT INTO `{#prefix#}ip` ( `uhash` ,`ip` ,`created`,`uid`)VALUES (?, ?,?,?)";
            DAO_Finder::exec($sql, DB_IP, array($uhash,$ip,time(),$uid));

            $sql    = "INSERT `{#prefix#}count_ip` (uhash,log_time,uid) VALUES(?,?,?)  ON DUPLICATE KEY UPDATE `ip`=ip+1";
            $time   = date('Ymd');
            DAO_Finder::exec($sql, DB_IP, array($uhash,$time,$uid));
        }

        //记录访问
        $acc    = DB_ACCESS::writeAccess($uhash);

        
        //FileWrite::writeTableAccess($sql);

        //记录今日pv
        DB_COUNT_PV::writeTodayPv($uhash);

        //记录操作系统
        $os = getOsName();
        //FileWrite::writeTableOs($uid, $uhash, $os);
        $sql= "INSERT {#prefix#}url_os (uid,uhash, name) values (?,?, ?) ON DUPLICATE KEY UPDATE click=click+1";
        DAO_Finder::exec($sql,DB_USER,array($uid, $uhash, $os));
        //来源
        //$referer    = getReferer();
        //$referer    = Common::pregDomain($rederer);;
        //FileWrite::writeTableReferer($uid, $uhash, $referer);
        
        //记录浏览器
        $ie = getIeName();
        //FileWrite::writeTableIe($uid, $uhash, $ie);
        $sql= "INSERT {#prefix#}url_ie (uid,uhash, name) values (?,?, ?) ON DUPLICATE KEY UPDATE click=click+1";
        DAO_Finder::exec($sql,DB_USER,array($uid, $uhash, $ie));
        
        //记录蜘蛛
        $spider = getRoots();
        //FileWrite::writeTableSpider($uid, $uhash, $spider);
        if(!empty($spider)) {
            $sql    = "INSERT {#prefix#}url_spider (uid,uhash, name) values (?,?,?) ON DUPLICATE KEY UPDATE click=click+1";
            DAO_Finder::exec($sql,  DB_USER, array($uid, $uhash, $spider));
        }


        
        $this->redirect($url, 302);
    }

    /**
     * 获取网址
     * @param <type> $uhash
     * @return string
     */
    public function getUrl($uhash) {
        $url = getHashUrl($uhash);
        if (empty($url)) {
            $cache = MemcacheAction::getCache();
            $url = $cache->get('url_' . $uhash);
            if (empty($url)) {
                $acc = DAO_Finder::findOne(DB_URL, array('uhash' => "$uhash"), 'url');
                if (!empty($acc)) {
                    $url = $acc->getUrl();
                } else {
                    return null;
                }
            }else{
                $url    = '/';
            }
        }
        return $url;
    }

    //月份统计
    public function mothCountBlock() {
        $endTime = strtotime(date("Y-m-d 23:59:59"));
        $startTime = $endTime - (3600 * 24 * 31);
        $date = array();
        for ($i = $startTime; $i <= $endTime; $i++) {
            $day = date('d', $i);
            $date[$day] = array('start' => strtotime(date('Y-m-d 00:00:00', $i)), 'end' => $i);
            $i = $i + (3600 * 24) - 1;
        }
    }

    /**
     * 获取新浪短网址
     * get_block('home', 'get_sina_url',array('url'=>'要缩短的网址'));
     */
    public function getSinaUrlBlock() {
        $baseUrl = 'http://'.$_SERVER['HTTP_HOST'].'/cgi/sina/short_url.php?q=' . base64_encode($this->params['url']);
        $request = new HTTP_Request($baseUrl);
        $request->sendRequest();
        $_SESSION['sina_url'] = trim($request->getResponseBody());
    }
    /**
     * 获取腾讯短网址
     * get_block('home', 'get_tencent_url',array('url'=>'要缩短的网址'));
     */
    public function getTencentUrlBlock() {
        $baseUrl = 'http://'.$_SERVER['HTTP_HOST'].'/cgi/tencent/short_url.php?q=' . base64_encode($this->params['url']);
        $request = new HTTP_Request($baseUrl);
        $request->sendRequest();
        $_SESSION['tencent_url'] = trim($request->getResponseBody());
    }

    public function setUrlUidBlock() {
        $uhash   = Cookies::getMine('u22_url');
        if(!empty($uhash)) {
            $uid    = $_SESSION['user']['id'];
            $uhash   = "'".str_replace(',', "','", $uhash)."'";
            $sql     = "update u22_url set uid=$uid where uhash in($uhash)";
            DAO_Finder::querySQl($sql);
            Cookies::remove('u22_url');
        }
    }

}

?>
